package com.viweb.storemanager.core;

import java.util.Vector;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * MySwing: Advanced Swing Utilites
 * Copyright (C) 2005  Santhosh Kumar T
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

public class ProgressMonitor {
    public static int TRUY_VAN_DU_LIEU = 0;
    public static int GOI_NHAN_DU_LIEU = 1;
    public static int GOI_DU_LIEU = 2;
    public static int NHAN_DU_LIEU = 3;
    public static int HIEN_THI_BAO_CAO = 4;
    
    int total, current = -1;
    boolean indeterminate;
    int milliSecondsToWait = 500; // half second
    String status;
    
    public ProgressMonitor(int total, boolean indeterminate, int milliSecondsToWait) {
        this.total = total;
        this.indeterminate = indeterminate;
        this.milliSecondsToWait = milliSecondsToWait;
    }
    
    public ProgressMonitor(int total, boolean indeterminate) {
        this.total = total;
        this.indeterminate = indeterminate;
    }
    
    public int getTotal() {
        return total;
    }
    
    public void start(int progressStyle) {
        
        if (current != -1) {
            throw new IllegalStateException("not started yet");
        }
        
        if (progressStyle == this.TRUY_VAN_DU_LIEU) {
            this.status = "Hệ thống đang đọc dữ liệu...                          ";
        } else if (progressStyle == this.GOI_NHAN_DU_LIEU) {
            this.status = "Gửi nhận dữ liệu...                                   ";
        } else if (progressStyle == this.GOI_DU_LIEU) {
            this.status = "Gửi dữ liệu...                                        ";
        } else if (progressStyle == this.NHAN_DU_LIEU) {
            this.status = "Nhận dữ liệu...                                       ";
        } else if (progressStyle == this.HIEN_THI_BAO_CAO) {
            this.status = "Hệ thống đang tổng hợp dữ liệu báo cáo...             ";
        }
        
        current = 0;
        fireChangeEvent();
    }
    
    public int getMilliSecondsToWait() {
        return milliSecondsToWait;
    }
    
    public int getCurrent() {
        return current;
    }
    
    public String getStatus() {
        return status;
    }
    
    public boolean isIndeterminate() {
        return indeterminate;
    }
    
    public void setCurrent(String status, int current) {
        
        if (current == -1) {
            throw new IllegalStateException("not started yet");
        }
        
        this.current = current;
        
        if (status!=null) {
            this.status = status;
        }
        
        fireChangeEvent();
    }
    
    /*--------------------------------[ ListenerSupport ]--------------------------------*/
    
    private Vector listeners = new Vector();
    private ChangeEvent ce = new ChangeEvent(this);
    
    public void addChangeListener(ChangeListener listener) {
        listeners.add(listener);
    }
    
    public void removeChangeListener(ChangeListener listener) {
        listeners.remove(listener);
    }
    
    private void fireChangeEvent() {
        ChangeListener listener[] =
                (ChangeListener[]) listeners.toArray(new ChangeListener[listeners.size()]);
        
        for (int i = 0; i < listener.length; i++) {
            listener[i].stateChanged(ce);
        }
    }
}
